﻿'use strict';

/* Service */
app.service('platformHttp',
['$http', '$window', '$q', '$state', '$modal', '$location',
function ($http, $window, $q, $state, $modal, $location) {
    var self = this, verNo = 1;
    var address = 'http://192.168.3.17:3080';
    this.post = function (_url, _data) {
        _url = address + '/' + _url + '.api';
        console.log(_url);
        var formData = new FormData();
        for (var item in _data || []) {
            if (typeof (_data[item]) == 'object')
                formData.append(item, JSON.stringify(_data[item]));
            else
                formData.append(item, _data[item]);
        }

        var ajaxPromise = $.ajax({
            url: _url,
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: "application/x-www-form-urlencoded",
            processData: false
        });
        var deferred = $q.defer();
        ajaxPromise.then(function (data) {
            data = angular.fromJson(data);
            console.log('httpPlus', _data.CallMethod || '', data);
            if (data.Code != 1) {
                deferred.reject(data.Msg);
            } else if (data.Code = 1) {
                deferred.resolve(data.Result);
            }
        }, deferred.reject, deferred.notify);
        return deferred.promise;
    };

    self.setApiAddress = function (_ip, _port) {
        address = 'http://' + _ip + ':' + _port;
        console.log(address);
    }
}]);

